package my.georings.dialog;

import my.georings.data.MyLocation;
import my.georings.expandable.LocationEditionAbstractActivity;


import android.media.RingtoneManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import my.georings.GeoPhoneRingsApp;
import my.georings.R;



public class EditDialog extends LocationEditionAbstractActivity implements OnClickListener{

	
	private MyLocation toEdit;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        
        int id = getIntent().getIntExtra("theId", -1);
        this.toEdit = ((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().getLocationOfId(id);
        
        super.onCreate(savedInstanceState);
    }

	@Override
	public void onClick(View v) {
		
		super.onClick(v);
		
		switch(v.getId()){
			
		case R.id.ButtonCancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		}
	}
	
	@Override
	protected void setListeners(){
		super.setParentListeners(this);
		findViewById(R.id.ButtonCancel).setOnClickListener(this);
	}

	

	@Override
	protected void saveChanges() {
		currentLocation.isValid(true);
		
		((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().changeLocation(currentLocation);
		
		setResult(RESULT_OK);
        finish();
        
        Toast toast = Toast.makeText(this, "Location was changed", Toast.LENGTH_SHORT);
		toast.show();
	}

	@Override
	protected void setAllViewsToDefault() {
		((TextView)findViewById(R.id.edit_name)).setText(toEdit.getName());
		((TextView)findViewById(R.id.TextViewLocationCurrent)).setText("latitude: "+(float)toEdit.getPoint().getLatitudeE6()/1000000+
				"\nlongitude: " + (float)toEdit.getPoint().getLongitudeE6()/1000000);
		
		((TextView)findViewById(R.id.TextViewRingCurrent)).setText(
				toEdit.getRing() == null ? "(none)" : RingtoneManager.getRingtone(this, toEdit.getRing()).getTitle(this));
		((TextView)findViewById(R.id.TextViewVolumeCurrent)).setText(toEdit.getVolume() >= 0 ? toEdit.getVolume()*10+"%" : "(default)");
		
		((ToggleButton)findViewById(R.id.ToggleButtonRing)).setChecked(toEdit.isChangeRing());
		((ToggleButton)findViewById(R.id.ToggleButtonVibra)).setChecked(toEdit.isVibra());
		((ToggleButton)findViewById(R.id.ToggleButtonVolume)).setChecked(toEdit.isChangeVolume());
		((ToggleButton)findViewById(R.id.ToggleButtonMute)).setChecked(toEdit.isMute());
		
		int radius = toEdit.getRadius();
		((TextView)findViewById(R.id.TextViewRadiusCurrent)).setText(	radius == 100 ? "100 m" : 
																	(radius == 200 ? "200 m" : "300 m"));
		
		changeViewsForMute(toEdit.isMute());
	}

	@Override
	protected void setContentView() {
		setContentView(R.layout.edit);
	}

	@Override
	protected void setCurrentLocation() {
		currentLocation = toEdit.copy();
	}

	

	
}
